<template>
  <div class="login-container">我是登录组件
    <div class="login-box">
      <div class="login-logo">
        <img src="../assets/logo.png" alt="">
      </div>
      <!--用户名登录区域-->
      
      <div class="login-input">
        <el-form :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
          <el-form-item prop="username">
            <el-input prefix-icon="iconfont icon-user" v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="iconfont icon-3702mima" v-model="loginForm.password" type="password"></el-input>
          </el-form-item>
        </el-form>
      </div>
      
      <div class="login-button">
        <el-button type="primary" @click="login">登陆</el-button>
        <el-button type="info" @click="resetLoginForm">重置</el-button>
      </div>
    </div>
  
  </div>
</template>

<script>
  export default {
    name: "Login",
    data() {
      return {
        loginForm: {
          username: 'admin',
          password: '123456'
        },
        loginFormRules: {
          username: [
            {required: true, message: '请输入用户名', trigger: 'blur'},
            {min: 3, max: 10, message: '长度在 3 到 10个字符', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            {min: 6, max: 15, message: '长度在 6 到 15个字符', trigger: 'blur'}
          ]
        }
      }
    },
    //实现表单的重置功能
    methods: {
      resetLoginForm() {
        //调用重置函数就行了
        this.$refs.loginFormRef.resetFields()
      },
      //表单登录前的验证
      login() {
        //函数接收一个返回值,如果验证通过返回true
        this.$refs.loginFormRef.validate(async valid => {
          if (!valid) return //说明验证失败
          const {data: result} = await this.$http.post('login', this.loginForm)
          if (result.meta.status !== 200) return this.$message.error('登录失败')
          this.$message.success('登录成功')
          //在登录成功的同时保存token
          window.sessionStorage.setItem('token', result.data.token)
          this.$router.push('/home')
        })
      }
    }
  }
</script>

<style scoped lang="less">
  .login-container {
    width: 100%;
    height: 100%;
    background-color: #2b4b6b;
    
    .login-box {
      width: 400px;
      height: 250px;
      background-color: #fff;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      
      .login-logo {
        width: 130px;
        height: 130px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 5px;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: #fff;
        
        img {
          width: 100%;
          height: 100%;
          border-radius: 50%;
          background-color: #ccc;
        }
      }
      
      .login-input {
        margin-top: 80px;
        padding: 0 10px;
      }
      
      .login-button {
        text-align: right;
        margin-right: 20px;
      }
      
    }
  }
</style>
